import java.util.Arrays;
import java.util.Scanner;

public class guessGame {
    public static void main(String[] args) {
        System.out.print("欢迎尝试猜字母游戏！");
        Scanner console = new Scanner(System.in);
        int time = 0;//总次数
        char[] arr = make();
        System.out.println(Arrays.toString(arr));
        System.out.print("游戏开始，请输入你所猜测的5个字母序列：（EXIT--退出）");
        String str;
        loop:while (true) {
            while (true) {
                str = console.nextLine();
                if (check(str)) {
                    break;
                }
            }
            char[] brr = change(str);
            int[] count = compare(arr, brr);
            if (count[0] == 5 && count[1] == 5) {
                System.out.println("恭喜你猜对了！你的得分是：" + (500 - time * 10));
                break loop;
            } else {
                time++;
                System.out.println("你猜对了" + count[0] + "个字符，其中" + count[1] + "个字符的位置正确!（总次数=" + time + " EXIT--退出）");
            }
        }
    }

    public static char[] make() {
        char[] arr = new char[5];
        for (int i = 0; i < 5; i++) {
            arr[i] = (char) ((int) (Math.random() * 26) + 'A');
            for (int j = 0; j < i; j++) {
                if (arr[i] == arr[j]) {
                    i--;
                    break;
                }
            }
        }
        return arr;
    }

    public static char[] change(String str) {
        char[] arr = new char[5];
        for (int i = 0; i < 5; i++) {
            arr[i] = str.charAt(i);
        }
        return arr;
    }

    public static int[] compare(char[] arr, char[] brr) {
        int[] count = new int[2];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < brr.length; j++) {
                if (arr[i] == brr[j]) {
                    count[0]++;
                    if (i == j) {
                        count[1]++;
                    }
                }
            }
        }
        return count;
    }

    public static boolean check(String str) {
        if (str.length() != 5) {
            if (str.equals("EXIT")) {
                System.out.println("谢谢你的尝试，再见！");
                System.exit(0);
                return true;
            } else {
                return false;
            }
        } else {
            char[] arr = change(str);
            for (int i = 0; i < arr.length - 1; i++) {
                for (int j = i + 1; j < arr.length; j++) {
                    if (arr[i] == arr[j]) {
                        return false;
                    }
                }
            }
            return true;
        }
    }
}
